#!/bin/bash

# 安装gcc
# yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison sqlite-devel iconv-devel

#redis压缩文件名
redis_file_name=$(ls ./ | grep redis-.*tar.gz)
#redis解压后文件名(截取字符串)
redis_dir=${redis_file_name%%.tar.gz}
#redis日志目录
redis_log1="/tmp/redis/7001"
redis_log2="/tmp/redis/7002"
redis_log3="/tmp/redis/7003"
redis_log4="/tmp/redis/7004"
redis_log5="/tmp/redis/7005"
redis_log6="/tmp/redis/7006"
echo "------------------------开始读取redis文件压缩包:${redis_file_name}------------------------"

# 判断redis目录是否存在，如果存在 表示已经编译过
if [ -d "./${redis_dir}" ];
then
  echo "------------------------./${redis_dir}目录已存在，不需要编译------------------------"
else
    echo "------------------------开始解压redis文件压缩包:${redis_file_name}------------------------"
    tar -zxvf ${redis_file_name}
    echo "------------------------完成解压redis文件压缩包:${redis_file_name}------------------------"
    echo "------------------------开始编译redis文件:${redis_dir}------------------------"
    cd ./${redis_dir}
    make
    make install
    cd ../
    echo "------------------------结束编译redis文件:${redis_dir}------------------------"
fi;

# 创建log目录
if [ -d "${redis_log1}" ];
then
    echo "------------------------${redis_log1}目录已存在,无需创建------------------------"
else
    echo "------------------------${redis_log1}目录不存在,创建------------------------"
    mkdir -p "${redis_log1}"
fi

if [ -d "${redis_log2}" ];
then
    echo "------------------------${redis_log2}目录已存在,无需创建------------------------"
else
    echo "------------------------${redis_log2}目录不存在,创建------------------------"
    mkdir -p "${redis_log2}"
fi

if [ -d "${redis_log3}" ];
then
    echo "------------------------${redis_log3}目录已存在,无需创建------------------------"
else
    echo "------------------------${redis_log3}目录不存在,创建------------------------"
    mkdir -p "${redis_log3}"
fi

if [ -d "${redis_log4}" ];
then
    echo "------------------------${redis_log4}目录已存在,无需创建------------------------"
else
    echo "------------------------${redis_log4}目录不存在,创建------------------------"
    mkdir -p "${redis_log4}"
fi

if [ -d "${redis_log5}" ];
then
    echo "------------------------${redis_log5}目录已存在,无需创建------------------------"
else
    echo "------------------------${redis_log5}目录不存在,创建------------------------"
    mkdir -p "${redis_log5}"
fi

if [ -d "${redis_log6}" ];
then
    echo "------------------------${redis_log6}目录已存在,无需创建------------------------"
else
    echo "------------------------${redis_log6}目录不存在,创建------------------------"
    mkdir -p "${redis_log6}"
fi

echo "------------------------开始修改配置文件------------------------"
cp ./${redis_dir}/redis.conf ${redis_log1}
cp ./${redis_dir}/redis.conf ${redis_log2}
cp ./${redis_dir}/redis.conf ${redis_log3}
cp ./${redis_dir}/redis.conf ${redis_log4}
cp ./${redis_dir}/redis.conf ${redis_log5}
cp ./${redis_dir}/redis.conf ${redis_log6}
sed  -i  's/bind 127.0.0.1/bind 0.0.0.0/g'  "${redis_log1}/redis.conf"
sed  -i  's/bind 127.0.0.1/bind 0.0.0.0/g'  "${redis_log2}/redis.conf"
sed  -i  's/bind 127.0.0.1/bind 0.0.0.0/g'  "${redis_log3}/redis.conf"
sed  -i  's/bind 127.0.0.1/bind 0.0.0.0/g'  "${redis_log4}/redis.conf"
sed  -i  's/bind 127.0.0.1/bind 0.0.0.0/g'  "${redis_log5}/redis.conf"
sed  -i  's/bind 127.0.0.1/bind 0.0.0.0/g'  "${redis_log6}/redis.conf"

sed  -i  's/port 6379/port 7001/g'  "${redis_log1}/redis.conf"
sed  -i  's/port 6379/port 7002/g'  "${redis_log2}/redis.conf"
sed  -i  's/port 6379/port 7003/g'  "${redis_log3}/redis.conf"
sed  -i  's/port 6379/port 7004/g'  "${redis_log4}/redis.conf"
sed  -i  's/port 6379/port 7005/g'  "${redis_log5}/redis.conf"
sed  -i  's/port 6379/port 7006/g'  "${redis_log6}/redis.conf"

sed  -i  's/daemonize no/daemonize yes/g'  "${redis_log1}/redis.conf"
sed  -i  's/daemonize no/daemonize yes/g'  "${redis_log2}/redis.conf"
sed  -i  's/daemonize no/daemonize yes/g'  "${redis_log3}/redis.conf"
sed  -i  's/daemonize no/daemonize yes/g'  "${redis_log4}/redis.conf"
sed  -i  's/daemonize no/daemonize yes/g'  "${redis_log5}/redis.conf"
sed  -i  's/daemonize no/daemonize yes/g'  "${redis_log6}/redis.conf"

sed  -i  's/# cluster-enabled yes/cluster-enabled yes/g'  "${redis_log1}/redis.conf"
sed  -i  's/# cluster-enabled yes/cluster-enabled yes/g'  "${redis_log2}/redis.conf"
sed  -i  's/# cluster-enabled yes/cluster-enabled yes/g'  "${redis_log3}/redis.conf"
sed  -i  's/# cluster-enabled yes/cluster-enabled yes/g'  "${redis_log4}/redis.conf"
sed  -i  's/# cluster-enabled yes/cluster-enabled yes/g'  "${redis_log5}/redis.conf"
sed  -i  's/# cluster-enabled yes/cluster-enabled yes/g'  "${redis_log6}/redis.conf"

sed  -i  's/\/var\/run\/redis_6379.pid/\/tmp\/redis\/7001\/redis_7001.pid/g'  "${redis_log1}/redis.conf"
sed  -i  's/\/var\/run\/redis_6379.pid/\/tmp\/redis\/7001\/redis_7002.pid/g'  "${redis_log2}/redis.conf"
sed  -i  's/\/var\/run\/redis_6379.pid/\/tmp\/redis\/7001\/redis_7003.pid/g'  "${redis_log3}/redis.conf"
sed  -i  's/\/var\/run\/redis_6379.pid/\/tmp\/redis\/7001\/redis_7004.pid/g'  "${redis_log4}/redis.conf"
sed  -i  's/\/var\/run\/redis_6379.pid/\/tmp\/redis\/7001\/redis_7005.pid/g'  "${redis_log5}/redis.conf"
sed  -i  's/\/var\/run\/redis_6379.pid/\/tmp\/redis\/7001\/redis_7006.pid/g'  "${redis_log6}/redis.conf"

sed  -i  's/logfile \"\"/logfile \/data\/log\/redis\/redis_7001.log/g'  "${redis_log1}/redis.conf"
sed  -i  's/logfile \"\"/logfile \/data\/log\/redis\/redis_7002.log/g'  "${redis_log2}/redis.conf"
sed  -i  's/logfile \"\"/logfile \/data\/log\/redis\/redis_7003.log/g'  "${redis_log3}/redis.conf"
sed  -i  's/logfile \"\"/logfile \/data\/log\/redis\/redis_7004.log/g'  "${redis_log4}/redis.conf"
sed  -i  's/logfile \"\"/logfile \/data\/log\/redis\/redis_7005.log/g'  "${redis_log5}/redis.conf"
sed  -i  's/logfile \"\"/logfile \/data\/log\/redis\/redis_7005.log/g'  "${redis_log6}/redis.conf"

sed  -i  's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7001.conf/g'  "${redis_log1}/redis.conf"
sed  -i  's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7002.conf/g'  "${redis_log2}/redis.conf"
sed  -i  's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7003.conf/g'  "${redis_log3}/redis.conf"
sed  -i  's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7004.conf/g'  "${redis_log4}/redis.conf"
sed  -i  's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7005.conf/g'  "${redis_log5}/redis.conf"
sed  -i  's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7006.conf/g'  "${redis_log6}/redis.conf"

echo "------------------------结束修改配置文件------------------------"

#echo "------------------------开始启动redis集群------------------------"
#
#echo "------------------------结束启动redis集群------------------------"

